package edu.joe.demo.controlller;

import edu.joe.demo.bean.Msg;
import edu.joe.demo.service.MibService;
import edu.joe.demo.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;


@CrossOrigin
@RestController
@RequestMapping(value = "mib")
public class MibController {

    @Autowired
    public MibService service;

    @RequestMapping(value = "getNodeTreeByMibs")
    public Msg getNodeTreeByMibs(String mibs){
        Assert.notNull(mibs, "获取不到mibs名字。");
        String[] arr = mibs.split(",");
        return service.getNodeTreeByMibs(Arrays.asList(arr));
    }

    @RequestMapping(value = "getNodeTree")
    public Msg getNodeTree(int[] ids){
        return service.getNodeTree(ids);
    }

    @RequestMapping(value = "uploadMibFile", method = RequestMethod.POST)
    public Msg uploadMibFile(@RequestParam("mibFile") MultipartFile mibFile) throws IOException {
        if(mibFile.isEmpty()){
            throw new IllegalArgumentException("获取不到mib文件。");
        }
        String txt = FileUtils.readAllText(mibFile.getInputStream());
        return service.uploadMibFile(txt);
    }

    @RequestMapping(value = "getAllMibs")
    public Msg getAllMibs(){
        return service.getAllMibs();
    }

    @RequestMapping(value = "deleteMibModule", method = RequestMethod.POST)
    public Msg deleteMib(int id){
        return service.deleteMib(id);
    }
}
